package com.kehutong.admin.controler;

import com.kehutong.admin.entity.CorpModuleVersion;
import com.kehutong.admin.service.CorpModuleVersionService;
import com.kehutong.common.errcode.Result;
import org.coraframework.bean.Beans;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.util.UUIDGenerator;

import java.io.IOException;
import java.util.Objects;

/**
 * 公司模块版本控制器
 *
 * @author: liukunlong
 * @date: 2021-07-28 11:52
 */
@MvcAction
@ReqMapping("/admin/sys/corp/module/version")
public class CorpModuleVersionController {
    static final Logger logger = LoggerFactory.getLogger(CorpModuleVersionController.class);

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private CorpModuleVersionService corpModuleVersionService;

    /**
     * 根据条件获取公司模板版本列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @ReqMapping("/page")
    public Page<CorpModuleVersion> page(JSONObject jsonObject) throws Exception {
        return corpModuleVersionService.page(jsonObject);
    }

    @ReqMapping("/get")
    public Object get(String companyNo) throws Exception{
        return corpModuleVersionService.get(companyNo);
    }


    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(CorpModuleVersion corpModuleVersion) {
        return corpModuleVersionService.save(corpModuleVersion);
    }


    @ReqMapping("/save/update")
    public Object saveOrUpdate(JSONObject jsonObject) throws Exception {
        String corpNo = jsonObject.getString("corpNo");
        CorpModuleVersion corpModuleVersion = jdbcSession.findOne(CorpModuleVersion.class).eq("corpNo", corpNo).exe();
        if (Objects.isNull(corpModuleVersion)) {
            corpModuleVersion = Pool.newInstance(CorpModuleVersion.class, UUIDGenerator.randomUUID());
            Beans.from(jsonObject).to(corpModuleVersion).exclude("id").igornNull().copy();
            corpModuleVersionService.save(corpModuleVersion);
        } else {
            Beans.from(jsonObject).to(corpModuleVersion).exclude("id").igornNull().copy();
            corpModuleVersionService.update(corpModuleVersion);
        }
        return Result.success();
    }

    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(CorpModuleVersion corpModuleVersion) throws Exception {
        return corpModuleVersionService.update(corpModuleVersion);
    }

    @ReqMapping("/delete")
    public Object delete(CorpModuleVersion corpModuleVersion) {
        return corpModuleVersionService.delete(corpModuleVersion);
    }
}
